package servlets;

import com.fasterxml.jackson.databind.ObjectMapper;
import dao.impl.DoctorDaoImpl;
import dao.impl.FeedBackDaoImpl;
import entity.Feedback;
import util.DateUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author:
 * @Date: 2020-05-10
 * @Description: @PACKAGE:${PACKAGE_NAME}
 * @version: 1.0
 */
@WebServlet(name = "异常信息上报", value = "/abnormalInformationFeedbackServlet")
public class AbnormalInformationFeedbackServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String json = "";
        ObjectMapper mapper = new ObjectMapper();
        Feedback feedback = new Feedback();
        DoctorDaoImpl doctorDao = new DoctorDaoImpl();
        FeedBackDaoImpl feedBackDao = new FeedBackDaoImpl();
        String id = request.getParameter("doctorId");
        id = id.substring(5, 23);
        String msg = request.getParameter("abnormalInformationFeedbackInformation");
        if (msg.trim().equals("")) {
            System.out.println("msg信息为空");
            json = mapper.writeValueAsString("上报失败");
        } else {
            feedback.setDoctorId(id);
            feedback.setfMassage(msg);
            feedback.setDoctorName(doctorDao.getByDoctorId(id).get(0).getName());
            feedback.setfDate(DateUtil.getStringDateShort());
            feedback.setFlag(0);
            feedback.setFrequency(0);
            int i = feedBackDao.add(feedback);
            System.out.println(i);
            json = mapper.writeValueAsString("上报成功");
        }
        response.setContentType("text/html;charset=UTF-8");
        mapper.writeValue(response.getWriter(), json);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
